﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml.Serialization;
using System.IO;
using System.Xml;

namespace Transformer.iHunter.UI
{
    public partial class FilterSettingsDialog : Form
    {
        private IFilterProvider provider;

        private FilterInfo settings;

        UserControl customPage;

        public FilterSettingsDialog(LocalClient client, IFilterProvider provider, FilterInfo settings)
        {
            InitializeComponent();

            this.commonPage.Client = client;
            this.provider = provider;
            this.settings = settings;
        }

        public FilterInfo FilterSettings
        {
            get
            {
                return settings;
            }

            set
            {
                settings = value;
                commonPage.ResetControlValues(settings);
            }
        }

        private void FilterSettingsDialog_Load(object sender, EventArgs e)
        {
            customPage = provider.GetFilterConfigureEditor();
            customPage.Dock = DockStyle.Fill;
            this.tabPageCustom.Controls.Add(customPage);

            IXmlSerializable ser = customPage as IXmlSerializable;
            using (TextReader rd = new StringReader(settings.Settings))
            using (XmlReader xreader = XmlReader.Create(rd))
            {
                ser.ReadXml(xreader);
            }
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            if (commonPage.FilterName.Length == 0)
            {
                MessageBox.Show(this,"Please input Filter name","iHunter", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            settings.Name = commonPage.FilterName;
            settings.Action = commonPage.Action;
            settings.FolderGuid = commonPage.MoveToFolderGuid;

            this.DialogResult = DialogResult.OK;
        }
    }
}
